<?php
namespace App\Api;
use PhalApi\Api;
use App\Domain\MirContent as Domain;

/**
 * 单页详情
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class MirContent extends Api {
    public function getRules() {
        return array(
            'Page' => array(
                'PageName' => array('name'=> 'PageName','require' => false, 'desc' => 'PageName'),
                'Category' => array('name'=> 'Category','require' => false, 'desc' => 'Category'),
                'CategoryTitle' => array('name'=> 'CategoryTitle','require' => false, 'desc' => 'CategoryTitle'),
            ),
            'Add' => array(
                'json' => array('name'=> 'json','require' => false, 'desc' => 'json'),
            )
        );
    }

    /**
     * 单页详情
     * @desc 默认接口服务，当未指定接口服务时执行此接口服务
     * @return string title 标题
     * @return string content 内容
     * @return string version 版本，格式：X.X.X
     * @return int time 当前时间戳
     * @exception 400 非法请求，参数传递错误
     */
    public function Page() {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $PageName = $this->PageName;
        $Category = $this->Category;
        $CategoryTitle = $this->CategoryTitle;



        $Domain = new Domain();
        $PageInfo = $Domain->selectByPageName($this);

        if( $PageInfo ){
            $url = $PageInfo['url'];
//        为了大义,宝石提亲
//        $url = 'https://v.qq.com/x/page/n3280ldowdb.html';
            $url_vid = preg_replace('/.*\/(.*)\.html/','$1',$url);

            $url = "http://vv.video.qq.com/getinfo?vids=$url_vid&platform=101001&charge=0&otype=json";
            $curl = new \PhalApi\CUrl();

            // 第二个参数，表示超时时间，单位为毫秒
            $rs = $curl->get($url, 3000);

            $rs = str_replace("QZOutputJson=",'',$rs);
            $rs = str_replace("};",'}',$rs);
            $rs = json_decode($rs,true);

            $v = $rs['vl']['vi'][0];
            $vid_url = $v['ul']['ui'][0]['url'].$v['fn']."?vkey=".$v['fvkey'];
            $data = array(
                'vid' => $vid_url,
                'content' => $PageInfo['content'],
            );

            return  $data;
        }

    }


    /**
     * 单页新增
     * @desc 默认接口服务，当未指定接口服务时执行此接口服务
     * @return string title 标题
     * @return string content 内容
     * @return string version 版本，格式：X.X.X
     * @return int time 当前时间戳
     * @exception 400 非法请求，参数传递错误
     */
    public function Add() {

        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

        $json = json_decode($this->json,true);

        $Domain = new Domain();
        return $Domain->insertPageName($json);
    }

}
